package co.yixiang.modules.system.service.impl;

import co.yixiang.modules.system.domain.Refund;
import co.yixiang.modules.system.domain.RefundDeposit;
import cn.hutool.core.convert.Convert;
import co.yixiang.common.service.impl.BaseServiceImpl;
import co.yixiang.modules.system.util.RefundUtils;
import lombok.AllArgsConstructor;
import com.github.pagehelper.PageInfo;
import co.yixiang.common.utils.QueryHelpPlus;
import co.yixiang.utils.FileUtil;
import co.yixiang.modules.system.service.RefundDepositService;
import co.yixiang.modules.system.dto.RefundDepositDto;
import co.yixiang.modules.system.dto.queryCriteria.RefundDepositQueryCriteria;
import co.yixiang.modules.system.mapper.RefundDepositMapper;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
 * @author zf
 * @date 2021-09-10
 */
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "refundDeposit")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class RefundDepositServiceImpl extends BaseServiceImpl<RefundDepositMapper, RefundDeposit> implements RefundDepositService {

    @Override
    //@Cacheable
    public Map<String, Object> queryAll(RefundDepositQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<RefundDeposit> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", Convert.toList(RefundDepositDto.class, page.getList()));
        map.put("totalElements", page.getTotal());
        return map;
    }


    @Override
    //@Cacheable
    public List<RefundDeposit> queryAll(RefundDepositQueryCriteria criteria) {
        return baseMapper.selectList(QueryHelpPlus.getPredicate(RefundDeposit.class, criteria));
    }


    @Override
    public void download(List<RefundDepositDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (RefundDepositDto refundDeposit : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("退款付款openid", refundDeposit.getOpenId());
            map.put("退款用户名", refundDeposit.getAccount());
            map.put("手机号", refundDeposit.getPhone());
            map.put("退款金额", refundDeposit.getMoney());
            map.put("微信支付订单号", refundDeposit.getTransactionId());
            map.put("商户支付订单号", refundDeposit.getOutTradeNo());
            map.put("商户退款单号", refundDeposit.getOutRefundNo());
            map.put("订单金额", refundDeposit.getTotalFee());
            map.put("退款金额	", refundDeposit.getRefundFee());
            map.put("退款原因", refundDeposit.getReason());
            map.put("退款状态 1退款中 2 退款成功。3退款失败", refundDeposit.getRefundStatus());
            map.put("创建时间", refundDeposit.getCreateTime());
            map.put("修改时间", refundDeposit.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }


    @Override
    public ResponseEntity<Object> audit(RefundDeposit refundDeposit, HttpServletRequest req) {
        if (refundDeposit.getCheckStatus() != 2) {
            this.updateById(refundDeposit);
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
        if (StringUtils.isEmpty(refundDeposit.getOutTradeNo())) {
            return new ResponseEntity<>("订单号不为空!", HttpStatus.BAD_REQUEST);
        }
        if (StringUtils.isEmpty(refundDeposit.getRefundFee())) {
            return new ResponseEntity<>("退款金额不能为空", HttpStatus.BAD_REQUEST);
        }
        Integer type = 1;
        Refund refund = new Refund();
        refund.setOut_refund_no(refundDeposit.getOutRefundNo());
        refund.setOut_trade_no(refundDeposit.getOutTradeNo());

        //订单金额转换微信支付单位分
        BigDecimal money = refundDeposit.getTotalFee().multiply(new BigDecimal(100));
        BigDecimal totalFee = money.setScale(0, BigDecimal.ROUND_HALF_UP);
        //退款金额转换微信支付单位分
        BigDecimal moneys = refundDeposit.getRefundFee().multiply(new BigDecimal(100));
        BigDecimal refundFee = moneys.setScale(0, BigDecimal.ROUND_HALF_UP);


        refund.setTotal_fee(totalFee.intValue());
        refund.setRefund_fee(refundFee.intValue());
        refund.setRefund_desc("正常退款");

        try {
            Map<String, Object> map = RefundUtils.refundUtil(refund, req, type);
            Boolean stu = (Boolean) map.get("stu");
            if (stu == false) {
                return new ResponseEntity<>("退款服务器异常", HttpStatus.BAD_REQUEST);
            }
//            RefundDeposit deposit = new RefundDeposit();
//            deposit.setId(refundDeposit.getId());
            refundDeposit.setRefundStatus(2);
            refundDeposit.setCheckStatus(2);
            this.updateById(refundDeposit);
            return new ResponseEntity<>("退款成功", HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>("服务器异常", HttpStatus.BAD_REQUEST);
        }
    }


}
